package org.juggers.client.ui;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.juggers.client.JuggersMessages;
import org.juggers.client.controller.EventController;
import org.juggers.domain.Event;
import org.juggers.domain.ModelState;
import org.juggers.validation.action.AlertAction;
import org.juggers.validation.action.ChangeStyleAction;
import org.juggers.validation.validator.MultiValidator;
import org.juggers.validation.validator.NotEmptyValidator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

/**
 * Event edition dialog box
 * 
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
@Singleton
public class EventDialogBox
    extends DialogBox
{
    @Inject
    private Provider<EventController> eventControllerProvider;

    @Inject
    private EventDialogBox( final ModelState model, JuggersMessages messages )
    {
        setText( "Evénement" );

        FlexTable layout = new FlexTable();
        layout.setCellSpacing( 10 );

        layout.setHTML( 0, 0, "Titre" );
        final TextBox title = new TextBox();
        layout.setWidget( 0, 1, title );

        layout.setHTML( 1, 0, "Description" );
        final TextArea summary = new TextArea();
        layout.setWidget( 1, 1, summary );

        layout.setHTML( 2, 0, "URL du logo" );
        final TextBox logo = new TextBox();
        layout.setWidget( 2, 1, logo );

        layout.setHTML( 3, 0, "Date" );
        final DateBox date = new DateBox();
        layout.setWidget( 3, 1, date );

        layout.setHTML( 4, 0, "Lieu" );
        final TextBox place = new TextBox();
        layout.setWidget( 4, 1, place );

        layout.setHTML( 5, 0, "Nombre de places" );
        final TextBox seats = new TextBox();
        layout.setWidget( 5, 1, seats );

        Button validate = new Button( messages.registerDialogBoxValidateButton() );
        layout.setWidget( 6, 0, validate );

        Button cancel = new Button( messages.registerDialogBoxCancelButton() );
        layout.setWidget( 6, 1, cancel );
        cancel.addClickHandler( new ClickHandler()
        {

            @Override
            public void onClick( ClickEvent arg0 )
            {
                eventControllerProvider.get().cancelEdit();
            }
        } );

        add( layout );

        final MultiValidator validator = new MultiValidator();
        validator.addValidator( new NotEmptyValidator( title ).addAction( new ChangeStyleAction( title ) ) );
        // validator.addValidator( new NotEmptyValidator( summary ).addAction( new ChangeStyleAction( summary ) ) );
        // validator.addValidator( new NotEmptyValidator( date ).addAction( new ChangeStyleAction( date ) ) );
        validator.addAction( new AlertAction( messages.registerDialogBoxValidationError() ) );

        validate.addClickHandler( new ClickHandler()
        {

            @Override
            public void onClick( ClickEvent arg0 )
            {
                if ( validator.validate() )
                {
                    Event event = model.getEvent();
                    event.setTitle( title.getText() );
                    event.setSummary( summary.getText() );
                    event.setLogo( logo.getText() );
                    event.setDateTime( date.getValue() );
                    event.setPlace( place.getValue() );
                    event.setMax( Integer.parseInt( seats.getText() ) );

                    if ( event.getId() == null )
                    {
                        eventControllerProvider.get().createEvent( event );
                    }
                    else
                    {
                        eventControllerProvider.get().update( event );
                    }
                }
            }
        } );
        
        model.addPropertyChangeListener( "event", new PropertyChangeListener()
        {
            
            @Override
            public void propertyChange( PropertyChangeEvent evt )
            {
                Event event = (Event) evt.getNewValue();
                if (event != null)
                {
                    title.setText( event.getTitle() );
                    summary.setText( event.getSummary() );
                    logo.setText( event.getLogo() );
                    date.setValue( event.getDateTime() );
                    place.setText( event.getPlace() );
                    seats.setText( String.valueOf( event.getMax() ) );
                }
            }
        });
    }
}
